<?php
namespace app\web\controller;
use app\common\controller\StoreBaseController;
use think\Db;
/**
 * 通用设置
 * 
 */
class GeneralSettingsController extends StoreBaseController {
	/**
	 * 收银设置
	 */
	public function index(){
		$store_id = session('storeInfo')['id'];
		$result = Db::name("mall_general_settings")->where($store_id)->find();
		$ss=$result['checkbox'];
		$res=unserialize($ss);
		if(isset($res['a'])){			
			$this->assign('res1',1);
		}else{
			$this->assign('res1',0);
		}
		if(isset($res['b'])){			
			$this->assign('res2',2);
		}else{
			$this->assign('res2',0);
		}
		if(isset($res['c'])){			
			$this->assign('res3',3);
		}else{
			$this->assign('res3',0);
		}
		if(isset($res['d'])){			
			$this->assign('res4',4);
		}else{
			$this->assign('res4',0);
		}
		$this->assign('result',$result);
		$result1 = Db::name("mall_general_settings")->where($store_id)->count('store_id');
		if($result1>0){
			if(request()->isPost()){			 	
				$param = input('post.'); 				
				//var_dump($res);die;
				$param['checkbox']=serialize($param['checkbox']); 
	         	if (Db::name("mall_general_settings")->where('store_id',$store_id)->update($param) !== false) {
	                    $this->redirect('index','');
	            } else {
	                $this->error('保存失败');
	            }	            
			}		
		}else{
			$result = Db::name("mall_general_settings")->where($store_id)->find();
			$this->assign('result',$result);
			if(request()->isPost()){
				$param = input('post.');  
				$param['store_id'] = $store_id; 			
				$param['checkbox']=serialize($param['checkbox']); 	
				if(Db::name("mall_general_settings")->insert($param)!== false){
				    $this->redirect('index','');
	            } else {
	                $this->error('添加失败');
	            }
			}	
		}
		
		return $this->fetch();
	}
	/*
	 * 积分规则
	 */
	public function integral_rule(){
		$store_id = session('storeInfo')['id'];
		$result = Db::name("mall_general_settings")->where($store_id)->find();
		$ss=$result['checkbox2'];
		$res=unserialize($ss);
		if(isset($res['a'])){			
			$this->assign('res1',1);
		}else{
			$this->assign('res1',0);
		}
		if(isset($res['b'])){			
			$this->assign('res2',2);
		}else{
			$this->assign('res2',0);
		}
		if(isset($res['c'])){			
			$this->assign('res3',3);
		}else{
			$this->assign('res3',0);
		}
		$this->assign('result',$result);
		$result1 = Db::name("mall_general_settings")->where($store_id)->count('store_id');
		if($result1>0){
			if(request()->isPost()){			 	
				$param = input('post.'); 				
				//var_dump($res);die;
				$param['checkbox2']=serialize($param['checkbox2']);		
	         	if (Db::name("mall_general_settings")->where('store_id',$store_id)->update($param) !== false) {
	                    $this->redirect('integral_rule','');
	            } else {
	                $this->error('保存失败');
	            }	            
			}		
		}else{
			$result = Db::name("mall_general_settings")->where($store_id)->find();
			$this->assign('result',$result);
			if(request()->isPost()){
				$param = input('post.');  
				$param['store_id'] = $store_id; 			
				$param['checkbox2']=serialize($param['checkbox2']); 	
				if(Db::name("mall_general_settings")->insert($param)!== false){
				    $this->redirect('integral_rule','');
	            } else {
	                $this->error('添加失败');
	            }
			}	
		}
		
		return $this->fetch();
	}
	/*
	 * 会员相关
	 */
	public function related_member(){	
		$store_id = session('storeInfo')['id'];
		$result = Db::name("mall_general_settings")->where($store_id)->find();
		$ss=$result['checkbox3'];
		$res=unserialize($ss);
		if(isset($res['a'])){			
			$this->assign('res1',1);
		}else{
			$this->assign('res1',0);
		}
		
		$this->assign('result',$result);
		$result1 = Db::name("mall_general_settings")->where($store_id)->count('store_id');
		if($result1>0){
			if(request()->isPost()){			 	
				$param = input('post.'); 				
				//var_dump($res);die;
				$param['checkbox3']=serialize($param['checkbox3']);		
	         	if (Db::name("mall_general_settings")->where('store_id',$store_id)->update($param) !== false) {
	                    $this->redirect('related_member','');
	            } else {
	                $this->error('保存失败');
	            }	            
			}		
		}else{
			$result = Db::name("mall_general_settings")->where($store_id)->find();
			$this->assign('result',$result);
			if(request()->isPost()){
				$param = input('post.');  
				$param['store_id'] = $store_id; 			
				$param['checkbox3']=serialize($param['checkbox3']); 	
				if(Db::name("mall_general_settings")->insert($param)!== false){
				    $this->redirect('related_member','');
	            } else {
	                $this->error('添加失败');
	            }
			}	
		}
		
		return $this->fetch();
	}
}
